/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package library;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

/**
 *
 * @author ryan
 */
public class XmlReader {
    public static Library getLibrary(File file) throws XPathExpressionException, ParserConfigurationException, org.xml.sax.SAXException, IOException
    {
       Library lib = new Library();

       Document xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
       XPath xpath = XPathFactory.newInstance().newXPath();
        
       NodeList users = (NodeList) xpath.evaluate("/library/user", xmlDoc, XPathConstants.NODESET);
        
       for(int i = 0; i < users.getLength(); i++)
           lib.addUser(parseUser(users.item(i), lib));
        
       NodeList items = (NodeList) xpath.evaluate("/library/item", xmlDoc, XPathConstants.NODESET);
        
       for(int i = 0; i < items.getLength(); i++)
           lib.addItem(parseItem(items.item(i)));
       
       return lib;
    }
    
    static private Item parseItem(Node itemNode) throws XPathExpressionException
    {
        int id = 0;
        String title = "";
        String desc = "";
        Item.Type t = Item.Type.DVD;
        Date due = null;
        Item item = null;

        XPath xpath = XPathFactory.newInstance().newXPath();
        id = Integer.parseInt(((Node) xpath.evaluate("id", itemNode, XPathConstants.NODE)).getTextContent());
        title = ((Node) xpath.evaluate("title", itemNode, XPathConstants.NODE)).getTextContent();
        desc = ((Node) xpath.evaluate("description", itemNode, XPathConstants.NODE)).getTextContent();
        String typeString = ((Node) xpath.evaluate("type", itemNode, XPathConstants.NODE)).getTextContent();
        Node dueNode = (Node) xpath.evaluate("due_date", itemNode, XPathConstants.NODE);
            
        if(dueNode != null)
            due = new Date(Long.parseLong(dueNode.getTextContent()));
            
        if(typeString.equalsIgnoreCase("BOOK"))
            t = Item.Type.BOOK;
        else if(typeString.equalsIgnoreCase("CHILDRENS_BOOK"))
            t = Item.Type.CHILDRENS_BOOK;
        else if(typeString.equalsIgnoreCase("VIDEOTAPE"))
            t = Item.Type.VIDEOTAPE;
            
        item = new Item(id, title, t, desc, due);      
        
        return item;
    }
    
    static private User parseUser(Node userNode, Library lib) throws XPathExpressionException
    {
        int id = 0;
        String name = "";
        User.Type t = User.Type.ADULT;
        User user = null;
        
        XPath xpath = XPathFactory.newInstance().newXPath();
        id = Integer.parseInt(((Node) xpath.evaluate("id", userNode, XPathConstants.NODE)).getTextContent());
        name = ((Node) xpath.evaluate("name", userNode, XPathConstants.NODE)).getTextContent();
        String typeString = ((Node) xpath.evaluate("type", userNode, XPathConstants.NODE)).getTextContent();

        if(typeString.equalsIgnoreCase("CHILD"))
            t = User.Type.CHILD;
            
        user = new User(id, name, t);
        
        NodeList items = (NodeList) xpath.evaluate("items/item", userNode, XPathConstants.NODESET);
            
        for(int i = 0; i < items.getLength(); i++)
        {
            Item item = parseItem(items.item(i));
            user.addItem(item);
            lib.addItem(item);
        }
        
        return user;
    }
}
